<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">






  
  
  <link rel="stylesheet" media="all" href="/blog/lib/Han/dist/han.min.css?v=3.3">




<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="数据结构," />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.2" />






<meta name="description" content="参考博文 AVL树的建立 对于一组关键码的输入序列，从空开始不断插入结点，最后构成AVL树 每插入一个结点后就应判断从该结点到根的路径上有无结点发生不平衡 如有不平衡问题，利用旋转方法进行树的调整，使之平衡化 建AVL树过程是不断插入结点和必要时进行平衡化的过程  树的高度：  左右孩子高度较大者的高度+1 节点类的代码实现： 1234567891011121314   // AVL树的节点(内部">
<meta name="keywords" content="数据结构">
<meta property="og:type" content="article">
<meta property="og:title" content="AVL平衡二叉搜索树">
<meta property="og:url" content="https://git.oschina.net/choichumgming/blog/2017/12/26/AVL平衡二叉搜索树/index.html">
<meta property="og:site_name" content="choichumgming&#39;s blog">
<meta property="og:description" content="参考博文 AVL树的建立 对于一组关键码的输入序列，从空开始不断插入结点，最后构成AVL树 每插入一个结点后就应判断从该结点到根的路径上有无结点发生不平衡 如有不平衡问题，利用旋转方法进行树的调整，使之平衡化 建AVL树过程是不断插入结点和必要时进行平衡化的过程  树的高度：  左右孩子高度较大者的高度+1 节点类的代码实现： 1234567891011121314   // AVL树的节点(内部">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://note.youdao.com/yws/res/1472/WEBRESOURCE8c4ebc10b3e2a4c34f90bc9fb56c169b">
<meta property="og:image" content="http://note.youdao.com/yws/res/1474/WEBRESOURCE621525b4954b6c380fd05dd58651948c">
<meta property="og:updated_time" content="2018-01-08T05:17:14.116Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="AVL平衡二叉搜索树">
<meta name="twitter:description" content="参考博文 AVL树的建立 对于一组关键码的输入序列，从空开始不断插入结点，最后构成AVL树 每插入一个结点后就应判断从该结点到根的路径上有无结点发生不平衡 如有不平衡问题，利用旋转方法进行树的调整，使之平衡化 建AVL树过程是不断插入结点和必要时进行平衡化的过程  树的高度：  左右孩子高度较大者的高度+1 节点类的代码实现： 1234567891011121314   // AVL树的节点(内部">
<meta name="twitter:image" content="http://note.youdao.com/yws/res/1472/WEBRESOURCE8c4ebc10b3e2a4c34f90bc9fb56c169b">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Gemini',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://git.oschina.net/choichumgming/blog/2017/12/26/AVL平衡二叉搜索树/"/>





  <title>AVL平衡二叉搜索树 | choichumgming's blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">choichumgming's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">stay foolish,stay hard</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/blog/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      
        
        <li class="menu-item menu-item-board">
          <a href="/blog/board/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-book"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://git.oschina.net/choichumgming/blog/blog/2017/12/26/AVL平衡二叉搜索树/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="choichumgming">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="choichumgming's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">AVL平衡二叉搜索树</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-12-26T15:09:14+08:00">
                2017-12-26
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/算法与数据结构/" itemprop="url" rel="index">
                    <span itemprop="name">算法与数据结构</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/blog/2017/12/26/AVL平衡二叉搜索树/#SOHUCS" itemprop="discussionUrl">
                  <span id="changyan_count_unit" class="post-comments-count hc-comment-count" data-xid="2017/12/26/AVL平衡二叉搜索树/" itemprop="commentsCount"></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        <p><a href="ttps://www.cnblogs.com/skywang12345/p/3577479.html" target="_blank" rel="external">参考博文</a></p>
<h2 id="AVL树的建立"><a href="#AVL树的建立" class="headerlink" title="AVL树的建立"></a>AVL树的建立</h2><ul>
<li>对于一组关键码的输入序列，从空开始不断插入结点，最后构成AVL树</li>
<li>每插入一个结点后就应判断从该结点到根的路径上有无结点发生不平衡</li>
<li>如有不平衡问题，利用旋转方法进行树的调整，使之平衡化</li>
<li>建AVL树过程是不断插入结点和必要时进行平衡化的过程</li>
</ul>
<p>树的高度：</p>
<hr>
<p>左右孩子高度较大者的高度+1<br> <strong>节点类的代码实现：</strong><br> <figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">   </div><div class="line"><span class="comment">// AVL树的节点(内部类)</span></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">AVLTreeNode</span>&lt;<span class="title">T</span> <span class="keyword">extends</span> <span class="title">Comparable</span>&lt;<span class="title">T</span>&gt;&gt; </span>&#123;</div><div class="line">    T key;                <span class="comment">// 关键字(键值)</span></div><div class="line">    <span class="keyword">int</span> height;         <span class="comment">// 高度</span></div><div class="line">    AVLTreeNode&lt;T&gt; left;    <span class="comment">// 左孩子</span></div><div class="line">    AVLTreeNode&lt;T&gt; right;    <span class="comment">// 右孩子</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">AVLTreeNode</span><span class="params">(T key, AVLTreeNode&lt;T&gt; left, AVLTreeNode&lt;T&gt; right)</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>.key = key;</div><div class="line">        <span class="keyword">this</span>.left = left;</div><div class="line">        <span class="keyword">this</span>.right = right;</div><div class="line">        <span class="keyword">this</span>.height = <span class="number">0</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h2 id="旋转"><a href="#旋转" class="headerlink" title="旋转"></a>旋转</h2><h3 id="LL的旋转"><a href="#LL的旋转" class="headerlink" title="LL的旋转"></a>LL的旋转</h3><p><img src="http://note.youdao.com/yws/res/1472/WEBRESOURCE8c4ebc10b3e2a4c34f90bc9fb56c169b" alt="enter image description here"><br>图中左边是旋转之前的树，右边是旋转之后的树。从中可以发现，旋转之后的树又变成了AVL树，而且该旋转只需要一次即可完成。<br>对于LL旋转，你可以这样理解为：LL旋转是围绕”失去平衡的AVL根节点”进行的，也就是节点k2；而且由于是LL情况，即左左情况，就用手抓着”左孩子，即k1”使劲摇。将k1变成根节点，k2变成k1的右子树，”k1的右子树”变成”k2的左子树”。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//代码实现：</span></div><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">     * LL：左左对应的情况(左单旋转)。</span></div><div class="line"><span class="comment">     *</span></div><div class="line"><span class="comment">     * 返回值：旋转后的根节点</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> AVLTreeNode&lt;T&gt; <span class="title">leftLeftRotation</span><span class="params">(AVLTreeNode&lt;T&gt; k2)</span> </span>&#123;</div><div class="line">        AVLTreeNode&lt;T&gt; k1;</div><div class="line"></div><div class="line">        k1 = k2.left;</div><div class="line">        k2.left = k1.right;</div><div class="line">        k1.right = k2;</div><div class="line"></div><div class="line">        k2.height = max( height(k2.left), height(k2.right)) + <span class="number">1</span>;</div><div class="line">        k1.height = max( height(k1.left), k2.height) + <span class="number">1</span>;</div><div class="line"></div><div class="line">        <span class="keyword">return</span> k1;</div><div class="line">    &#125;</div><div class="line"></div><div class="line"><span class="comment">//RR的旋转同上：</span></div><div class="line"><span class="comment">//代码实现：</span></div><div class="line"> <span class="comment">/*</span></div><div class="line"><span class="comment">     * RR：右右对应的情况(右单旋转)。</span></div><div class="line"><span class="comment">     *</span></div><div class="line"><span class="comment">     * 返回值：旋转后的根节点</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> AVLTreeNode&lt;T&gt; <span class="title">rightRightRotation</span><span class="params">(AVLTreeNode&lt;T&gt; k1)</span> </span>&#123;</div><div class="line">        AVLTreeNode&lt;T&gt; k2;</div><div class="line"></div><div class="line">        k2 = k1.right;</div><div class="line">        k1.right = k2.left;</div><div class="line">        k2.left = k1;</div><div class="line"></div><div class="line">        k1.height = max( height(k1.left), height(k1.right)) + <span class="number">1</span>;</div><div class="line">        k2.height = max( height(k2.right), k1.height) + <span class="number">1</span>;</div><div class="line"></div><div class="line">        <span class="keyword">return</span> k2;</div><div class="line">    &#125;</div></pre></td></tr></table></figure>
<h3 id="LR的旋转"><a href="#LR的旋转" class="headerlink" title="LR的旋转"></a>LR的旋转</h3><p>LR失去平衡的情况，需要经过两次旋转才能让AVL树恢复平衡。<br><img src="http://note.youdao.com/yws/res/1474/WEBRESOURCE621525b4954b6c380fd05dd58651948c" alt="enter image description here"><br>第一次旋转是围绕”k1”进行的”RR旋转”，第二次是围绕”k3”进行的”LL旋转”。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">代码实现：</div><div class="line"> <span class="comment">/*</span></div><div class="line"><span class="comment">     * LR：左右对应的情况(左双旋转)。</span></div><div class="line"><span class="comment">     *</span></div><div class="line"><span class="comment">     * 返回值：旋转后的根节点</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> AVLTreeNode&lt;T&gt; <span class="title">leftRightRotation</span><span class="params">(AVLTreeNode&lt;T&gt; k3)</span> </span>&#123;</div><div class="line">        k3.left = rightRightRotation(k3.left);</div><div class="line">        <span class="keyword">return</span> leftLeftRotation(k3);</div><div class="line">    &#125;</div><div class="line">RL的旋转同上：</div><div class="line">代码实现：</div><div class="line">    <span class="comment">/*</span></div><div class="line"><span class="comment">     * RL：右左对应的情况(右双旋转)。</span></div><div class="line"><span class="comment">     *</span></div><div class="line"><span class="comment">     * 返回值：旋转后的根节点</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> AVLTreeNode&lt;T&gt; <span class="title">rightLeftRotation</span><span class="params">(AVLTreeNode&lt;T&gt; k1)</span> </span>&#123;</div><div class="line">        k1.right = leftLeftRotation(k1.right);</div><div class="line"></div><div class="line">        <span class="keyword">return</span> rightRightRotation(k1);</div><div class="line">    &#125;</div></pre></td></tr></table></figure></p>
<h2 id="插入"><a href="#插入" class="headerlink" title="插入"></a>插入</h2><p>从根一直找到叶子节点，并用数组记住他走过的路。将新加入的节点加入到尾部。先更新子树的高度，再以此更新祖父节点的因子，更新到的第一个因子不为0，-1，1的节点，从这个节点往后找两个节点。判断这三个节点是什么形状，LR，RL，RR，LL这四种类型的哪种类型。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/* </span></div><div class="line"><span class="comment">    * 将结点插入到AVL树中，并返回根节点</span></div><div class="line"><span class="comment">    *</span></div><div class="line"><span class="comment">    * 参数说明：</span></div><div class="line"><span class="comment">    *     tree AVL树的根结点</span></div><div class="line"><span class="comment">    *     key 插入的结点的键值</span></div><div class="line"><span class="comment">    * 返回值：</span></div><div class="line"><span class="comment">    *     根节点</span></div><div class="line"><span class="comment">    */</span></div><div class="line">   <span class="function"><span class="keyword">private</span> AVLTreeNode&lt;T&gt; <span class="title">insert</span><span class="params">(AVLTreeNode&lt;T&gt; tree, T key)</span> </span>&#123;</div><div class="line">       <span class="keyword">if</span> (tree == <span class="keyword">null</span>) &#123;</div><div class="line">           <span class="comment">// 新建节点</span></div><div class="line">           tree = <span class="keyword">new</span> AVLTreeNode&lt;T&gt;(key, <span class="keyword">null</span>, <span class="keyword">null</span>);</div><div class="line">           <span class="keyword">if</span> (tree==<span class="keyword">null</span>) &#123;</div><div class="line">               System.out.println(<span class="string">"ERROR: create avltree node failed!"</span>);</div><div class="line">               <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line">           &#125;</div><div class="line">       &#125; <span class="keyword">else</span> &#123;</div><div class="line">           <span class="keyword">int</span> cmp = key.compareTo(tree.key);<span class="comment">//此时判断是是要插入左子树还是右子树</span></div><div class="line"></div><div class="line">              <span class="keyword">if</span> (cmp &lt; <span class="number">0</span>) &#123;    <span class="comment">// 应该将key插入到"tree的左子树"的情况</span></div><div class="line">               tree.left = insert(tree.left, key);</div><div class="line">               <span class="comment">// 插入节点后，若AVL树失去平衡，则进行相应的调节。</span></div><div class="line">               <span class="keyword">if</span> (height(tree.left) - height(tree.right) &gt;= <span class="number">2</span> || height(tree.left) - height(tree.right) &lt;=- <span class="number">2</span>) &#123;</div><div class="line">                   <span class="keyword">if</span> (key.compareTo(tree.left.key) &lt; <span class="number">0</span>)<span class="comment">//通过判断插入的位置，选择选择方式</span></div><div class="line">                       tree = leftLeftRotation(tree);</div><div class="line">                   <span class="keyword">else</span></div><div class="line">                       tree = leftRightRotation(tree);</div><div class="line">               &#125;</div><div class="line">           &#125; <span class="keyword">else</span> <span class="keyword">if</span> (cmp &gt; <span class="number">0</span>) &#123;    <span class="comment">// 应该将key插入到"tree的右子树"的情况</span></div><div class="line">               tree.right = insert(tree.right, key);</div><div class="line">               <span class="comment">// 插入节点后，若AVL树失去平衡，则进行相应的调节。</span></div><div class="line">               <span class="keyword">if</span> (height(tree.right) - height(tree.left) == <span class="number">2</span>) &#123;</div><div class="line">                   <span class="keyword">if</span> (key.compareTo(tree.right.key) &gt; <span class="number">0</span>)</div><div class="line">                       tree = rightRightRotation(tree);</div><div class="line">                   <span class="keyword">else</span></div><div class="line">                       tree = rightLeftRotation(tree);</div><div class="line">               &#125;</div><div class="line">           &#125; <span class="keyword">else</span> &#123;    <span class="comment">// cmp==0</span></div><div class="line">               System.out.println(<span class="string">"添加失败：不允许添加相同的节点！"</span>);</div><div class="line">           &#125;</div><div class="line">       &#125;</div><div class="line"></div><div class="line">       tree.height = max( height(tree.left), height(tree.right)) + <span class="number">1</span>;</div><div class="line"></div><div class="line">       <span class="keyword">return</span> tree;</div><div class="line">   &#125;</div></pre></td></tr></table></figure>
<h2 id="删除"><a href="#删除" class="headerlink" title="删除"></a>删除</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/* </span></div><div class="line"><span class="comment">    * 删除结点(z)，返回根节点</span></div><div class="line"><span class="comment">    *</span></div><div class="line"><span class="comment">    * 参数说明：</span></div><div class="line"><span class="comment">    *     tree AVL树的根结点</span></div><div class="line"><span class="comment">    *     z 待删除的结点</span></div><div class="line"><span class="comment">    * 返回值：</span></div><div class="line"><span class="comment">    *     根节点</span></div><div class="line"><span class="comment">    */</span></div><div class="line">   <span class="function"><span class="keyword">private</span> AVLTreeNode&lt;T&gt; <span class="title">remove</span><span class="params">(AVLTreeNode&lt;T&gt; tree, AVLTreeNode&lt;T&gt; z)</span> </span>&#123;</div><div class="line">       <span class="comment">// 根为空 或者 没有要删除的节点，直接返回null。</span></div><div class="line">       <span class="keyword">if</span> (tree==<span class="keyword">null</span> || z==<span class="keyword">null</span>)</div><div class="line">           <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line"></div><div class="line">       <span class="keyword">int</span> cmp = z.key.compareTo(tree.key);</div><div class="line">       <span class="keyword">if</span> (cmp &lt; <span class="number">0</span>) &#123;        <span class="comment">// 待删除的节点在"tree的左子树"中</span></div><div class="line">           tree.left = remove(tree.left, z);</div><div class="line">           <span class="comment">// 删除节点后，若AVL树失去平衡，则进行相应的调节。</span></div><div class="line">           <span class="keyword">if</span> (height(tree.right) - height(tree.left) == <span class="number">2</span>) &#123;</div><div class="line">               AVLTreeNode&lt;T&gt; r =  tree.right;</div><div class="line">               <span class="keyword">if</span> (height(r.left) &gt; height(r.right))</div><div class="line">                   tree = rightLeftRotation(tree);</div><div class="line">               <span class="keyword">else</span></div><div class="line">                   tree = rightRightRotation(tree);</div><div class="line">           &#125;</div><div class="line">       &#125; <span class="keyword">else</span> <span class="keyword">if</span> (cmp &gt; <span class="number">0</span>) &#123;    <span class="comment">// 待删除的节点在"tree的右子树"中</span></div><div class="line">           tree.right = remove(tree.right, z);</div><div class="line">           <span class="comment">// 删除节点后，若AVL树失去平衡，则进行相应的调节。</span></div><div class="line">           <span class="keyword">if</span> (height(tree.left) - height(tree.right) == <span class="number">2</span>) &#123;</div><div class="line">               AVLTreeNode&lt;T&gt; l =  tree.left;</div><div class="line">               <span class="keyword">if</span> (height(l.right) &gt; height(l.left))</div><div class="line">                   tree = leftRightRotation(tree);</div><div class="line">               <span class="keyword">else</span></div><div class="line">                   tree = leftLeftRotation(tree);</div><div class="line">           &#125;</div><div class="line">       &#125; <span class="keyword">else</span> &#123;    <span class="comment">// tree是对应要删除的节点。</span></div><div class="line">           <span class="comment">// tree的左右孩子都非空</span></div><div class="line">           <span class="keyword">if</span> ((tree.left!=<span class="keyword">null</span>) &amp;&amp; (tree.right!=<span class="keyword">null</span>)) &#123;</div><div class="line">               <span class="keyword">if</span> (height(tree.left) &gt; height(tree.right)) &#123;</div><div class="line">                   <span class="comment">// 如果tree的左子树比右子树高；</span></div><div class="line">                   <span class="comment">// 则(01)找出tree的左子树中的最大节点</span></div><div class="line">                   <span class="comment">//   (02)将该最大节点的值赋值给tree。</span></div><div class="line">                   <span class="comment">//   (03)删除该最大节点。</span></div><div class="line">                   <span class="comment">// 这类似于用"tree的左子树中最大节点"做"tree"的替身；</span></div><div class="line">                   <span class="comment">// 采用这种方式的好处是：删除"tree的左子树中最大节点"之后，AVL树仍然是平衡的。</span></div><div class="line">                   AVLTreeNode&lt;T&gt; max = maximum(tree.left);</div><div class="line">                   tree.key = max.key;</div><div class="line">                   tree.left = remove(tree.left, max);</div><div class="line">               &#125; <span class="keyword">else</span> &#123;</div><div class="line">                   <span class="comment">// 如果tree的左子树不比右子树高(即它们相等，或右子树比左子树高1)</span></div><div class="line">                   <span class="comment">// 则(01)找出tree的右子树中的最小节点</span></div><div class="line">                   <span class="comment">//   (02)将该最小节点的值赋值给tree。</span></div><div class="line">                   <span class="comment">//   (03)删除该最小节点。</span></div><div class="line">                   <span class="comment">// 这类似于用"tree的右子树中最小节点"做"tree"的替身；</span></div><div class="line">                   <span class="comment">// 采用这种方式的好处是：删除"tree的右子树中最小节点"之后，AVL树仍然是平衡的。</span></div><div class="line">                   AVLTreeNode&lt;T&gt; min = maximum(tree.right);</div><div class="line">                   tree.key = min.key;</div><div class="line">                   tree.right = remove(tree.right, min);</div><div class="line">               &#125;</div><div class="line">           &#125; <span class="keyword">else</span> &#123;</div><div class="line">               AVLTreeNode&lt;T&gt; tmp = tree;</div><div class="line">               tree = (tree.left!=<span class="keyword">null</span>) ? tree.left : tree.right;</div><div class="line">               tmp = <span class="keyword">null</span>;</div><div class="line">           &#125;</div><div class="line">       &#125;</div><div class="line"></div><div class="line">       <span class="keyword">return</span> tree;</div><div class="line">   &#125;</div></pre></td></tr></table></figure>
<h2 id="完整代码"><a href="#完整代码" class="headerlink" title="完整代码"></a>完整代码</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div><div class="line">208</div><div class="line">209</div><div class="line">210</div><div class="line">211</div><div class="line">212</div><div class="line">213</div><div class="line">214</div><div class="line">215</div><div class="line">216</div><div class="line">217</div><div class="line">218</div><div class="line">219</div><div class="line">220</div><div class="line">221</div><div class="line">222</div><div class="line">223</div><div class="line">224</div><div class="line">225</div><div class="line">226</div><div class="line">227</div><div class="line">228</div><div class="line">229</div><div class="line">230</div><div class="line">231</div><div class="line">232</div><div class="line">233</div><div class="line">234</div><div class="line">235</div><div class="line">236</div><div class="line">237</div><div class="line">238</div><div class="line">239</div><div class="line">240</div><div class="line">241</div><div class="line">242</div><div class="line">243</div><div class="line">244</div><div class="line">245</div><div class="line">246</div><div class="line">247</div><div class="line">248</div><div class="line">249</div><div class="line">250</div><div class="line">251</div><div class="line">252</div><div class="line">253</div><div class="line">254</div><div class="line">255</div><div class="line">256</div><div class="line">257</div><div class="line">258</div><div class="line">259</div><div class="line">260</div><div class="line">261</div><div class="line">262</div><div class="line">263</div><div class="line">264</div><div class="line">265</div><div class="line">266</div><div class="line">267</div><div class="line">268</div><div class="line">269</div><div class="line">270</div><div class="line">271</div><div class="line">272</div><div class="line">273</div><div class="line">274</div><div class="line">275</div><div class="line">276</div><div class="line">277</div><div class="line">278</div><div class="line">279</div><div class="line">280</div><div class="line">281</div><div class="line">282</div><div class="line">283</div><div class="line">284</div><div class="line">285</div><div class="line">286</div><div class="line">287</div><div class="line">288</div><div class="line">289</div><div class="line">290</div><div class="line">291</div><div class="line">292</div><div class="line">293</div><div class="line">294</div><div class="line">295</div><div class="line">296</div><div class="line">297</div><div class="line">298</div><div class="line">299</div><div class="line">300</div><div class="line">301</div><div class="line">302</div><div class="line">303</div><div class="line">304</div><div class="line">305</div><div class="line">306</div><div class="line">307</div><div class="line">308</div><div class="line">309</div><div class="line">310</div><div class="line">311</div><div class="line">312</div><div class="line">313</div><div class="line">314</div><div class="line">315</div><div class="line">316</div><div class="line">317</div><div class="line">318</div><div class="line">319</div><div class="line">320</div><div class="line">321</div><div class="line">322</div><div class="line">323</div><div class="line">324</div><div class="line">325</div><div class="line">326</div><div class="line">327</div><div class="line">328</div><div class="line">329</div><div class="line">330</div><div class="line">331</div><div class="line">332</div><div class="line">333</div><div class="line">334</div><div class="line">335</div><div class="line">336</div><div class="line">337</div><div class="line">338</div><div class="line">339</div><div class="line">340</div><div class="line">341</div><div class="line">342</div><div class="line">343</div><div class="line">344</div><div class="line">345</div><div class="line">346</div><div class="line">347</div><div class="line">348</div><div class="line">349</div><div class="line">350</div><div class="line">351</div><div class="line">352</div><div class="line">353</div><div class="line">354</div><div class="line">355</div><div class="line">356</div><div class="line">357</div><div class="line">358</div><div class="line">359</div><div class="line">360</div><div class="line">361</div><div class="line">362</div><div class="line">363</div><div class="line">364</div><div class="line">365</div><div class="line">366</div><div class="line">367</div><div class="line">368</div><div class="line">369</div><div class="line">370</div><div class="line">371</div><div class="line">372</div><div class="line">373</div><div class="line">374</div><div class="line">375</div><div class="line">376</div><div class="line">377</div><div class="line">378</div><div class="line">379</div><div class="line">380</div><div class="line">381</div><div class="line">382</div><div class="line">383</div><div class="line">384</div><div class="line">385</div><div class="line">386</div><div class="line">387</div><div class="line">388</div><div class="line">389</div><div class="line">390</div><div class="line">391</div><div class="line">392</div><div class="line">393</div><div class="line">394</div><div class="line">395</div><div class="line">396</div><div class="line">397</div><div class="line">398</div><div class="line">399</div><div class="line">400</div><div class="line">401</div><div class="line">402</div><div class="line">403</div><div class="line">404</div><div class="line">405</div><div class="line">406</div><div class="line">407</div><div class="line">408</div><div class="line">409</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"><span class="comment"> * Java 语言: AVL树</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * <span class="doctag">@author</span> skywang</span></div><div class="line"><span class="comment"> * <span class="doctag">@date</span> 2013/11/07</span></div><div class="line"><span class="comment"> */</span></div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AVLTree</span>&lt;<span class="title">T</span> <span class="keyword">extends</span> <span class="title">Comparable</span>&lt;<span class="title">T</span>&gt;&gt; </span>&#123;</div><div class="line">    <span class="keyword">private</span> AVLTreeNode&lt;T&gt; mRoot;    <span class="comment">// 根结点</span></div><div class="line"></div><div class="line">    <span class="comment">// AVL树的节点(内部类)</span></div><div class="line">    <span class="class"><span class="keyword">class</span> <span class="title">AVLTreeNode</span>&lt;<span class="title">T</span> <span class="keyword">extends</span> <span class="title">Comparable</span>&lt;<span class="title">T</span>&gt;&gt; </span>&#123;</div><div class="line">        T key;                <span class="comment">// 关键字(键值)</span></div><div class="line">        <span class="keyword">int</span> height;         <span class="comment">// 高度</span></div><div class="line">        AVLTreeNode&lt;T&gt; left;    <span class="comment">// 左孩子</span></div><div class="line">        AVLTreeNode&lt;T&gt; right;    <span class="comment">// 右孩子</span></div><div class="line"></div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="title">AVLTreeNode</span><span class="params">(T key, AVLTreeNode&lt;T&gt; left, AVLTreeNode&lt;T&gt; right)</span> </span>&#123;</div><div class="line">            <span class="keyword">this</span>.key = key;</div><div class="line">            <span class="keyword">this</span>.left = left;</div><div class="line">            <span class="keyword">this</span>.right = right;</div><div class="line">            <span class="keyword">this</span>.height = <span class="number">0</span>;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">// 构造函数</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">AVLTree</span><span class="params">()</span> </span>&#123;</div><div class="line">        mRoot = <span class="keyword">null</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/*</span></div><div class="line"><span class="comment">     * 获取树的高度</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">int</span> <span class="title">height</span><span class="params">(AVLTreeNode&lt;T&gt; tree)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (tree != <span class="keyword">null</span>)</div><div class="line">            <span class="keyword">return</span> tree.height;</div><div class="line"></div><div class="line">        <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">height</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> height(mRoot);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/*</span></div><div class="line"><span class="comment">     * 比较两个值的大小</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">int</span> <span class="title">max</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> a&gt;b ? a : b;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/*</span></div><div class="line"><span class="comment">     * 前序遍历"AVL树"</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">preOrder</span><span class="params">(AVLTreeNode&lt;T&gt; tree)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span>(tree != <span class="keyword">null</span>) &#123;</div><div class="line">            System.out.print(tree.key+<span class="string">" "</span>);</div><div class="line">            preOrder(tree.left);</div><div class="line">            preOrder(tree.right);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">preOrder</span><span class="params">()</span> </span>&#123;</div><div class="line">        preOrder(mRoot);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/*</span></div><div class="line"><span class="comment">     * 中序遍历"AVL树"</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">inOrder</span><span class="params">(AVLTreeNode&lt;T&gt; tree)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span>(tree != <span class="keyword">null</span>)</div><div class="line">        &#123;</div><div class="line">            inOrder(tree.left);</div><div class="line">            System.out.print(tree.key+<span class="string">" "</span>);</div><div class="line">            inOrder(tree.right);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">inOrder</span><span class="params">()</span> </span>&#123;</div><div class="line">        inOrder(mRoot);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/*</span></div><div class="line"><span class="comment">     * 后序遍历"AVL树"</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">postOrder</span><span class="params">(AVLTreeNode&lt;T&gt; tree)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span>(tree != <span class="keyword">null</span>) &#123;</div><div class="line">            postOrder(tree.left);</div><div class="line">            postOrder(tree.right);</div><div class="line">            System.out.print(tree.key+<span class="string">" "</span>);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">postOrder</span><span class="params">()</span> </span>&#123;</div><div class="line">        postOrder(mRoot);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/*</span></div><div class="line"><span class="comment">     * (递归实现)查找"AVL树x"中键值为key的节点</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> AVLTreeNode&lt;T&gt; <span class="title">search</span><span class="params">(AVLTreeNode&lt;T&gt; x, T key)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (x==<span class="keyword">null</span>)</div><div class="line">            <span class="keyword">return</span> x;</div><div class="line"></div><div class="line">        <span class="keyword">int</span> cmp = key.compareTo(x.key);</div><div class="line">        <span class="keyword">if</span> (cmp &lt; <span class="number">0</span>)</div><div class="line">            <span class="keyword">return</span> search(x.left, key);</div><div class="line">        <span class="keyword">else</span> <span class="keyword">if</span> (cmp &gt; <span class="number">0</span>)</div><div class="line">            <span class="keyword">return</span> search(x.right, key);</div><div class="line">        <span class="keyword">else</span></div><div class="line">            <span class="keyword">return</span> x;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> AVLTreeNode&lt;T&gt; <span class="title">search</span><span class="params">(T key)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> search(mRoot, key);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/*</span></div><div class="line"><span class="comment">     * (非递归实现)查找"AVL树x"中键值为key的节点</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> AVLTreeNode&lt;T&gt; <span class="title">iterativeSearch</span><span class="params">(AVLTreeNode&lt;T&gt; x, T key)</span> </span>&#123;</div><div class="line">        <span class="keyword">while</span> (x!=<span class="keyword">null</span>) &#123;</div><div class="line">            <span class="keyword">int</span> cmp = key.compareTo(x.key);</div><div class="line"></div><div class="line">            <span class="keyword">if</span> (cmp &lt; <span class="number">0</span>)</div><div class="line">                x = x.left;</div><div class="line">            <span class="keyword">else</span> <span class="keyword">if</span> (cmp &gt; <span class="number">0</span>)</div><div class="line">                x = x.right;</div><div class="line">            <span class="keyword">else</span></div><div class="line">                <span class="keyword">return</span> x;</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="keyword">return</span> x;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> AVLTreeNode&lt;T&gt; <span class="title">iterativeSearch</span><span class="params">(T key)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> iterativeSearch(mRoot, key);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/* </span></div><div class="line"><span class="comment">     * 查找最小结点：返回tree为根结点的AVL树的最小结点。</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> AVLTreeNode&lt;T&gt; <span class="title">minimum</span><span class="params">(AVLTreeNode&lt;T&gt; tree)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (tree == <span class="keyword">null</span>)</div><div class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line"></div><div class="line">        <span class="keyword">while</span>(tree.left != <span class="keyword">null</span>)</div><div class="line">            tree = tree.left;</div><div class="line">        <span class="keyword">return</span> tree;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> T <span class="title">minimum</span><span class="params">()</span> </span>&#123;</div><div class="line">        AVLTreeNode&lt;T&gt; p = minimum(mRoot);</div><div class="line">        <span class="keyword">if</span> (p != <span class="keyword">null</span>)</div><div class="line">            <span class="keyword">return</span> p.key;</div><div class="line"></div><div class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line">    &#125;</div><div class="line">     </div><div class="line">    <span class="comment">/* </span></div><div class="line"><span class="comment">     * 查找最大结点：返回tree为根结点的AVL树的最大结点。</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> AVLTreeNode&lt;T&gt; <span class="title">maximum</span><span class="params">(AVLTreeNode&lt;T&gt; tree)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (tree == <span class="keyword">null</span>)</div><div class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line"></div><div class="line">        <span class="keyword">while</span>(tree.right != <span class="keyword">null</span>)</div><div class="line">            tree = tree.right;</div><div class="line">        <span class="keyword">return</span> tree;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> T <span class="title">maximum</span><span class="params">()</span> </span>&#123;</div><div class="line">        AVLTreeNode&lt;T&gt; p = maximum(mRoot);</div><div class="line">        <span class="keyword">if</span> (p != <span class="keyword">null</span>)</div><div class="line">            <span class="keyword">return</span> p.key;</div><div class="line"></div><div class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/*</span></div><div class="line"><span class="comment">     * LL：左左对应的情况(左单旋转)。</span></div><div class="line"><span class="comment">     *</span></div><div class="line"><span class="comment">     * 返回值：旋转后的根节点</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> AVLTreeNode&lt;T&gt; <span class="title">leftLeftRotation</span><span class="params">(AVLTreeNode&lt;T&gt; k2)</span> </span>&#123;</div><div class="line">        AVLTreeNode&lt;T&gt; k1;</div><div class="line"></div><div class="line">        k1 = k2.left;</div><div class="line">        k2.left = k1.right;</div><div class="line">        k1.right = k2;</div><div class="line"></div><div class="line">        k2.height = max( height(k2.left), height(k2.right)) + <span class="number">1</span>;</div><div class="line">        k1.height = max( height(k1.left), k2.height) + <span class="number">1</span>;</div><div class="line"></div><div class="line">        <span class="keyword">return</span> k1;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/*</span></div><div class="line"><span class="comment">     * RR：右右对应的情况(右单旋转)。</span></div><div class="line"><span class="comment">     *</span></div><div class="line"><span class="comment">     * 返回值：旋转后的根节点</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> AVLTreeNode&lt;T&gt; <span class="title">rightRightRotation</span><span class="params">(AVLTreeNode&lt;T&gt; k1)</span> </span>&#123;</div><div class="line">        AVLTreeNode&lt;T&gt; k2;</div><div class="line"></div><div class="line">        k2 = k1.right;</div><div class="line">        k1.right = k2.left;</div><div class="line">        k2.left = k1;</div><div class="line"></div><div class="line">        k1.height = max( height(k1.left), height(k1.right)) + <span class="number">1</span>;</div><div class="line">        k2.height = max( height(k2.right), k1.height) + <span class="number">1</span>;</div><div class="line"></div><div class="line">        <span class="keyword">return</span> k2;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/*</span></div><div class="line"><span class="comment">     * LR：左右对应的情况(左双旋转)。</span></div><div class="line"><span class="comment">     *</span></div><div class="line"><span class="comment">     * 返回值：旋转后的根节点</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> AVLTreeNode&lt;T&gt; <span class="title">leftRightRotation</span><span class="params">(AVLTreeNode&lt;T&gt; k3)</span> </span>&#123;</div><div class="line">        k3.left = rightRightRotation(k3.left);</div><div class="line"></div><div class="line">        <span class="keyword">return</span> leftLeftRotation(k3);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/*</span></div><div class="line"><span class="comment">     * RL：右左对应的情况(右双旋转)。</span></div><div class="line"><span class="comment">     *</span></div><div class="line"><span class="comment">     * 返回值：旋转后的根节点</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> AVLTreeNode&lt;T&gt; <span class="title">rightLeftRotation</span><span class="params">(AVLTreeNode&lt;T&gt; k1)</span> </span>&#123;</div><div class="line">        k1.right = leftLeftRotation(k1.right);</div><div class="line"></div><div class="line">        <span class="keyword">return</span> rightRightRotation(k1);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/* </span></div><div class="line"><span class="comment">     * 将结点插入到AVL树中，并返回根节点</span></div><div class="line"><span class="comment">     *</span></div><div class="line"><span class="comment">     * 参数说明：</span></div><div class="line"><span class="comment">     *     tree AVL树的根结点</span></div><div class="line"><span class="comment">     *     key 插入的结点的键值</span></div><div class="line"><span class="comment">     * 返回值：</span></div><div class="line"><span class="comment">     *     根节点</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> AVLTreeNode&lt;T&gt; <span class="title">insert</span><span class="params">(AVLTreeNode&lt;T&gt; tree, T key)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (tree == <span class="keyword">null</span>) &#123;</div><div class="line">            <span class="comment">// 新建节点</span></div><div class="line">            tree = <span class="keyword">new</span> AVLTreeNode&lt;T&gt;(key, <span class="keyword">null</span>, <span class="keyword">null</span>);</div><div class="line">            <span class="keyword">if</span> (tree==<span class="keyword">null</span>) &#123;</div><div class="line">                System.out.println(<span class="string">"ERROR: create avltree node failed!"</span>);</div><div class="line">                <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line">            &#125;</div><div class="line">        &#125; <span class="keyword">else</span> &#123;</div><div class="line">            <span class="keyword">int</span> cmp = key.compareTo(tree.key);</div><div class="line"></div><div class="line">               <span class="keyword">if</span> (cmp &lt; <span class="number">0</span>) &#123;    <span class="comment">// 应该将key插入到"tree的左子树"的情况</span></div><div class="line">                tree.left = insert(tree.left, key);</div><div class="line">                <span class="comment">// 插入节点后，若AVL树失去平衡，则进行相应的调节。</span></div><div class="line">                <span class="keyword">if</span> (height(tree.left) - height(tree.right) == <span class="number">2</span>) &#123;</div><div class="line">                    <span class="keyword">if</span> (key.compareTo(tree.left.key) &lt; <span class="number">0</span>)</div><div class="line">                        tree = leftLeftRotation(tree);</div><div class="line">                    <span class="keyword">else</span></div><div class="line">                        tree = leftRightRotation(tree);</div><div class="line">                &#125;</div><div class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (cmp &gt; <span class="number">0</span>) &#123;    <span class="comment">// 应该将key插入到"tree的右子树"的情况</span></div><div class="line">                tree.right = insert(tree.right, key);</div><div class="line">                <span class="comment">// 插入节点后，若AVL树失去平衡，则进行相应的调节。</span></div><div class="line">                <span class="keyword">if</span> (height(tree.right) - height(tree.left) == <span class="number">2</span>) &#123;</div><div class="line">                    <span class="keyword">if</span> (key.compareTo(tree.right.key) &gt; <span class="number">0</span>)</div><div class="line">                        tree = rightRightRotation(tree);</div><div class="line">                    <span class="keyword">else</span></div><div class="line">                        tree = rightLeftRotation(tree);</div><div class="line">                &#125;</div><div class="line">            &#125; <span class="keyword">else</span> &#123;    <span class="comment">// cmp==0</span></div><div class="line">                System.out.println(<span class="string">"添加失败：不允许添加相同的节点！"</span>);</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        tree.height = max( height(tree.left), height(tree.right)) + <span class="number">1</span>;</div><div class="line"></div><div class="line">        <span class="keyword">return</span> tree;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">insert</span><span class="params">(T key)</span> </span>&#123;</div><div class="line">        mRoot = insert(mRoot, key);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/* </span></div><div class="line"><span class="comment">     * 删除结点(z)，返回根节点</span></div><div class="line"><span class="comment">     *</span></div><div class="line"><span class="comment">     * 参数说明：</span></div><div class="line"><span class="comment">     *     tree AVL树的根结点</span></div><div class="line"><span class="comment">     *     z 待删除的结点</span></div><div class="line"><span class="comment">     * 返回值：</span></div><div class="line"><span class="comment">     *     根节点</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> AVLTreeNode&lt;T&gt; <span class="title">remove</span><span class="params">(AVLTreeNode&lt;T&gt; tree, AVLTreeNode&lt;T&gt; z)</span> </span>&#123;</div><div class="line">        <span class="comment">// 根为空 或者 没有要删除的节点，直接返回null。</span></div><div class="line">        <span class="keyword">if</span> (tree==<span class="keyword">null</span> || z==<span class="keyword">null</span>)</div><div class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line"></div><div class="line">        <span class="keyword">int</span> cmp = z.key.compareTo(tree.key);</div><div class="line">        <span class="keyword">if</span> (cmp &lt; <span class="number">0</span>) &#123;        <span class="comment">// 待删除的节点在"tree的左子树"中</span></div><div class="line">            tree.left = remove(tree.left, z);</div><div class="line">            <span class="comment">// 删除节点后，若AVL树失去平衡，则进行相应的调节。</span></div><div class="line">            <span class="keyword">if</span> (height(tree.right) - height(tree.left) == <span class="number">2</span>) &#123;</div><div class="line">                AVLTreeNode&lt;T&gt; r =  tree.right;</div><div class="line">                <span class="keyword">if</span> (height(r.left) &gt; height(r.right))</div><div class="line">                    tree = rightLeftRotation(tree);</div><div class="line">                <span class="keyword">else</span></div><div class="line">                    tree = rightRightRotation(tree);</div><div class="line">            &#125;</div><div class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (cmp &gt; <span class="number">0</span>) &#123;    <span class="comment">// 待删除的节点在"tree的右子树"中</span></div><div class="line">            tree.right = remove(tree.right, z);</div><div class="line">            <span class="comment">// 删除节点后，若AVL树失去平衡，则进行相应的调节。</span></div><div class="line">            <span class="keyword">if</span> (height(tree.left) - height(tree.right) == <span class="number">2</span>) &#123;</div><div class="line">                AVLTreeNode&lt;T&gt; l =  tree.left;</div><div class="line">                <span class="keyword">if</span> (height(l.right) &gt; height(l.left))</div><div class="line">                    tree = leftRightRotation(tree);</div><div class="line">                <span class="keyword">else</span></div><div class="line">                    tree = leftLeftRotation(tree);</div><div class="line">            &#125;</div><div class="line">        &#125; <span class="keyword">else</span> &#123;    <span class="comment">// tree是对应要删除的节点。</span></div><div class="line">            <span class="comment">// tree的左右孩子都非空</span></div><div class="line">            <span class="keyword">if</span> ((tree.left!=<span class="keyword">null</span>) &amp;&amp; (tree.right!=<span class="keyword">null</span>)) &#123;</div><div class="line">                <span class="keyword">if</span> (height(tree.left) &gt; height(tree.right)) &#123;</div><div class="line">                    <span class="comment">// 如果tree的左子树比右子树高；</span></div><div class="line">                    <span class="comment">// 则(01)找出tree的左子树中的最大节点</span></div><div class="line">                    <span class="comment">//   (02)将该最大节点的值赋值给tree。</span></div><div class="line">                    <span class="comment">//   (03)删除该最大节点。</span></div><div class="line">                    <span class="comment">// 这类似于用"tree的左子树中最大节点"做"tree"的替身；</span></div><div class="line">                    <span class="comment">// 采用这种方式的好处是：删除"tree的左子树中最大节点"之后，AVL树仍然是平衡的。</span></div><div class="line">                    AVLTreeNode&lt;T&gt; max = maximum(tree.left);</div><div class="line">                    tree.key = max.key;</div><div class="line">                    tree.left = remove(tree.left, max);</div><div class="line">                &#125; <span class="keyword">else</span> &#123;</div><div class="line">                    <span class="comment">// 如果tree的左子树不比右子树高(即它们相等，或右子树比左子树高1)</span></div><div class="line">                    <span class="comment">// 则(01)找出tree的右子树中的最小节点</span></div><div class="line">                    <span class="comment">//   (02)将该最小节点的值赋值给tree。</span></div><div class="line">                    <span class="comment">//   (03)删除该最小节点。</span></div><div class="line">                    <span class="comment">// 这类似于用"tree的右子树中最小节点"做"tree"的替身；</span></div><div class="line">                    <span class="comment">// 采用这种方式的好处是：删除"tree的右子树中最小节点"之后，AVL树仍然是平衡的。</span></div><div class="line">                    AVLTreeNode&lt;T&gt; min = maximum(tree.right);</div><div class="line">                    tree.key = min.key;</div><div class="line">                    tree.right = remove(tree.right, min);</div><div class="line">                &#125;</div><div class="line">            &#125; <span class="keyword">else</span> &#123;</div><div class="line">                AVLTreeNode&lt;T&gt; tmp = tree;</div><div class="line">                tree = (tree.left!=<span class="keyword">null</span>) ? tree.left : tree.right;</div><div class="line">                tmp = <span class="keyword">null</span>;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="keyword">return</span> tree;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">remove</span><span class="params">(T key)</span> </span>&#123;</div><div class="line">        AVLTreeNode&lt;T&gt; z; </div><div class="line"></div><div class="line">        <span class="keyword">if</span> ((z = search(mRoot, key)) != <span class="keyword">null</span>)</div><div class="line">            mRoot = remove(mRoot, z);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/* </span></div><div class="line"><span class="comment">     * 销毁AVL树</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">destroy</span><span class="params">(AVLTreeNode&lt;T&gt; tree)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (tree==<span class="keyword">null</span>)</div><div class="line">            <span class="keyword">return</span> ;</div><div class="line"></div><div class="line">        <span class="keyword">if</span> (tree.left != <span class="keyword">null</span>)</div><div class="line">            destroy(tree.left);</div><div class="line">        <span class="keyword">if</span> (tree.right != <span class="keyword">null</span>)</div><div class="line">            destroy(tree.right);</div><div class="line"></div><div class="line">        tree = <span class="keyword">null</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">destroy</span><span class="params">()</span> </span>&#123;</div><div class="line">        destroy(mRoot);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/*</span></div><div class="line"><span class="comment">     * 打印"二叉查找树"</span></div><div class="line"><span class="comment">     *</span></div><div class="line"><span class="comment">     * key        -- 节点的键值 </span></div><div class="line"><span class="comment">     * direction  --  0，表示该节点是根节点;</span></div><div class="line"><span class="comment">     *               -1，表示该节点是它的父结点的左孩子;</span></div><div class="line"><span class="comment">     *                1，表示该节点是它的父结点的右孩子。</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">print</span><span class="params">(AVLTreeNode&lt;T&gt; tree, T key, <span class="keyword">int</span> direction)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span>(tree != <span class="keyword">null</span>) &#123;</div><div class="line">            <span class="keyword">if</span>(direction==<span class="number">0</span>)    <span class="comment">// tree是根节点</span></div><div class="line">                System.out.printf(<span class="string">"%2d is root\n"</span>, tree.key, key);</div><div class="line">            <span class="keyword">else</span>                <span class="comment">// tree是分支节点</span></div><div class="line">                System.out.printf(<span class="string">"%2d is %2d's %6s child\n"</span>, tree.key, key, direction==<span class="number">1</span>?<span class="string">"right"</span> : <span class="string">"left"</span>);</div><div class="line"></div><div class="line">            print(tree.left, tree.key, -<span class="number">1</span>);</div><div class="line">            print(tree.right,tree.key,  <span class="number">1</span>);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">print</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (mRoot != <span class="keyword">null</span>)</div><div class="line">            print(mRoot, mRoot.key, <span class="number">0</span>);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/数据结构/" rel="tag"># 数据结构</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2017/12/25/队列与栈/" rel="next" title="栈和队列">
                <i class="fa fa-chevron-left"></i> 栈和队列
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2017/12/26/排序/" rel="prev" title="排序">
                排序 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <!--PC版-->
<div id="SOHUCS" ></div>


    

  </div>
     <div class="comments" id="comments">
       
     </div>
   





        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/avatar.gif"
               alt="choichumgming" />
          <p class="site-author-name" itemprop="name">choichumgming</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives/">
                <span class="site-state-item-count">15</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories/index.html">
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              
                <span class="site-state-item-count">6</span>
                <span class="site-state-item-name">标签</span>
              
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="http://www.weibo.com/u/2558763801" target="_blank" title="微博">
                  
                    <i class="fa fa-fw fa-weibo"></i>
                  
                    
                      微博
                    
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#AVL树的建立"><span class="nav-number">1.</span> <span class="nav-text">AVL树的建立</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#旋转"><span class="nav-number">2.</span> <span class="nav-text">旋转</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#LL的旋转"><span class="nav-number">2.1.</span> <span class="nav-text">LL的旋转</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#LR的旋转"><span class="nav-number">2.2.</span> <span class="nav-text">LR的旋转</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#插入"><span class="nav-number">3.</span> <span class="nav-text">插入</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#删除"><span class="nav-number">4.</span> <span class="nav-text">删除</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#完整代码"><span class="nav-number">5.</span> <span class="nav-text">完整代码</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">choichumgming</span>

  
</div>


  <div class="powered-by">
    由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
  </div>

  <span class="post-meta-divider">|</span>

  <div class="theme-info">
    主题 &mdash;
    <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
      NexT.Gemini
    </a>
  </div>


        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.2"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.2"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.2"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.2"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  




  
    <script type="text/javascript">
    (function(){
      var appid = 'cytnT5dky';
      var conf = '56c170b7352bd027682e3707e9eb5071';
      var width = window.innerWidth || document.documentElement.clientWidth;
      if (width < 960) {
      window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="https://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>'); } else { var loadJs=function(d,a){var c=document.getElementsByTagName("head")[0]||document.head||document.documentElement;var b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("charset","UTF-8");b.setAttribute("src",d);if(typeof a==="function"){if(window.attachEvent){b.onreadystatechange=function(){var e=b.readyState;if(e==="loaded"||e==="complete"){b.onreadystatechange=null;a()}}}else{b.onload=a}}c.appendChild(b)};loadJs("https://changyan.sohu.com/upload/changyan.js",function(){
        window.changyan.api.config({appid:appid,conf:conf})});
      }
    })();
    </script>
    <script type="text/javascript" src="https://assets.changyan.sohu.com/upload/plugins/plugins.count.js"></script>
  





  





  

  

  

  

  

  

</body>
</html>
